package cn.zsb.config;

import cn.zsb.web.intercept.RoleInterceptor;
import cn.zsb.web.intercept.TimeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.config.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;

/**
 * @author oldliu
 * @since 1.0
 */
@Configuration
//@EnableWebMvc
public class ZsbConfig implements WebMvcConfigurer {
    //    @Override
////    public void addResourceHandlers(ResourceHandlerRegistry registry) {
////  registry.addResourceHandler("swagger-ui")
////                .addResourceLocations("classpath:/META-INF/resources/");
////        registry.addResourceHandler("doc.html")
////                .addResourceLocations("classpath:/META-INF/resources/");
////        registry.addResourceHandler("/webjars/**")
////                .addResourceLocations("classpath:/META-INF/resources/webjars/");
////    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路劲
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOrigins("*")//其他版本用这个
                // .allowedOriginPatterns("*")2.6以上用这个
                //设置允许的方法
                .allowedMethods("*")
                //允许的请求头
                .allowedHeaders("*")
                //跨域允许时间
                .maxAge(3600)
                //是否允许证书 不在默认开启
                .allowCredentials(true);
        ///.exposedHeaders("Authorization");//?
    }

    ///@Autowired
    @Resource(name = "stringRedisTemplate")
    private RedisTemplate stringRedisTemplate;

    @Override//不需要@bean注解
    public void addInterceptors(InterceptorRegistry registry) {
        TimeInterceptor t = new TimeInterceptor();//"/students.html","/js/**",
        registry.addInterceptor(t).excludePathPatterns("/**.css", "/**/**.js", "/**.html", "/**.jpg", "/**.png").addPathPatterns("/**");

        //设置redis
        RoleInterceptor role=new RoleInterceptor();//"/students.html","/js/**",
        role.setRedisTemplate(stringRedisTemplate);
//
//
//       // registry.addInterceptor(role).excludePathPatterns("**example**","/.*v3.*","/.*jar.*","/.*swagger.*","/
// .*/user/reg").addPathPatterns("/**");
        registry.addInterceptor(role).excludePathPatterns("/**/*.html","/adv/**","**example**",
 "/swagger-resources","/swagger-resources/**","/webjars/*","/v3/**",
                "/swagger-ui/**","/**.css","/**/**.js","/**.html","/**.jpg","/**.png","/user/login","/**/user/reg").addPathPatterns("/**");

    }
}
